package com.num2.service.wly;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import com.num2.dao.wly.IChartDao;
import com.num2.model.wly.BalanceChartModel;
import com.num2.model.wly.RichListChartModel;

@Service
public class ChartService {
	@Resource
	SqlSessionFactory ssf;
	
	public BalanceChartModel getBalanceChart(Integer user_id){
		//开启会话
		SqlSession sqlSession = ssf.openSession();
		//获取mybatis实例的dao对象
		IChartDao chartDao = sqlSession.getMapper(IChartDao.class);
		//通过id查询用户的余额与日期的集合
		List<Map<String,Object>> res =chartDao.getBalanceDateById(user_id);
		//生成日期数组/余额数组
		BalanceChartModel bc = new BalanceChartModel();
		for(Map<String, Object> r : res){
			bc.getDate().add((String)r.get("transaction_date"));
			bc.getBalance().add((Integer)r.get("balance"));
		}
		//转换成json格式
		bc.setAttr2Json();
		return bc; 
	}
	/**
	 * 
	 * @return
	 */
	public RichListChartModel getRichListChart(){
		//开启会话
		SqlSession sqlSession = ssf.openSession();
		//获取mybatis实例的dao对象
		IChartDao chartDao = sqlSession.getMapper(IChartDao.class);
		//查询余额排名前十位的用户
		List<Map<String,Object>> res = chartDao.getRichListChart();
		//关闭会话
		sqlSession.close();
		//用户id，余额，用户名数组
		RichListChartModel rlcm = new RichListChartModel();
		for(Map<String,Object> map : res){
			rlcm.getUser_id().add((Integer) map.get("user_id"));
			rlcm.getBalance().add((Integer) map.get("balance"));
			rlcm.getName().add((String) map.get("name"));
		}
		//json格式转换
		rlcm.setAttr2Json();
		return rlcm;
	}
	
	public SqlSessionFactory getSsf() {
		return ssf;
	}

	public void setSsf(SqlSessionFactory ssf) {
		this.ssf = ssf;
	}

}